package com.zcpt.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zcpt.dao.ProjectDao;
import com.zcpt.exception.AlterException;
import com.zcpt.exception.InsertException;
import com.zcpt.exception.SystemException;
import com.zcpt.pojo.Project;
import com.zcpt.service.ProjectService;

@Service("projectService")
public class ProjectServiceImpl implements ProjectService {

	@Resource
	private ProjectDao projectDao;

	@Override // 根据id查找项目信息
	public Project getProjectById(int projectId) {
		return projectDao.queryProjectById(projectId);
	}

	@Override // 根据id查找项目和参与者信息
	public Project getProjectByIdWithPartner(int projectId) {
		return projectDao.queryProjectByIdWithPartner(projectId);
	}

	@Override // 查找所有项目信息
	public List<Project> getAllProject() {
		return projectDao.queryAllProject();
	}

	@Override // 是否成功添加项目信息
	public boolean addProject(Project project) {
		try {
			int insertCount = projectDao.insertProject(project);
			if (insertCount <= 0) {
				throw new InsertException("添加数据失败！");
			} else {
				return true;
			}
		} catch (InsertException e) {
			throw e;
		} catch (Exception e) {
			throw new SystemException("系统故障:" + e.getMessage());
		}
	}

	@Override // 项目是否成功关联项目和参与者数据
	public boolean projectPartner(int projectId, int partnerId) {
		try {
			int withCount = projectDao.insertProjectWithPartner(projectId, partnerId);
			if (withCount <= 0) {
				throw new InsertException("关联数据失败！");
			} else {
				return true;
			}
		} catch (InsertException e) {
			throw e;
		} catch (Exception e) {
			throw new SystemException("系统故障:" + e.getMessage());
		}
	}

	@Override // 是否成功修改项目信息
	public boolean alterProject(Project project) {
		try {
			int updateCount = projectDao.updateProject(project);
			if (updateCount <= 0) {
				throw new AlterException("修改数据失败！");
			} else {
				return true;
			}
		} catch (AlterException e) {
			throw e;
		} catch (Exception e) {
			throw new SystemException("系统故障:" + e.getMessage());
		}
	}

}
